<template>
  <div class="adminhead">
    <img class="img-head" src="/public/adminIco.png" alt="xjtu" />
  </div>
  <div class="adminbody">
    <a-tabs
      v-model:activeKey="activeKey"
      style="
        width: 40%;
        min-width: 500px;
        max-width: 500px;
        box-shadow: 0px 0px 10px 10px rgba(0, 0, 0, 0.2);
        border-radius: 5px;
        padding: 20px;
        height: 430px;
        background-color: #00000068;
      "
    >
      <a-tab-pane key="1" tab="密码登录">
        <a-form :model="user" :wrapper-col="wrapperCol">
          <h1
            style="
              text-align: center;
              font-family: 楷体;
              font-size: 45px;
              font-weight: bold;
              color: #ffffff;
              margin: 30px auto;
            "
          >
            Login
          </h1>
          <a-form-item>
            <a-input v-model:value="user.u_id">
              <template #prefix><user-outlined type="user" /></template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-password v-model:value="user.u_pwd">
              <template #prefix><lock-outlined /></template>
            </a-input-password>
          </a-form-item>

          <a-form-item :wrapper-col="wrapperCol" style="padding-bottom: 50px">
            <a-button type="primary" block @click="login">登录</a-button>
          </a-form-item>
        </a-form>
      </a-tab-pane>
      <a-tab-pane key="2" tab="短信登录">
        <a-form :model="tel" :wrapper-col="wrapperCol">
          <h1
            style="
              text-align: center;
              font-family: 楷体;
              font-size: 45px;
              font-weight: bold;
              color: #ffffff;
              margin: 30px auto;
            "
          >
            Login
          </h1>
          <a-form-item>
            <a-input v-model:value="tel.u_tel" placeholder="input your tel">
              <template #prefix><mobile-outlined /></template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-search
              v-model:value="tel.code"
              placeholder="input the code"
              @search="getTelVerificationCode()"
            >
              <template #prefix><lock-outlined /></template>
              <template #enterButton>
                <a-button type="primary" v-if="tel_code" disabled
                  >已获得验证码</a-button
                >
                <a-button type="primary" v-else>获得验证码</a-button>
              </template>
            </a-input-search>
          </a-form-item>

          <a-form-item :wrapper-col="wrapperCol" style="padding-bottom: 50px">
            <a-button type="primary" block @click="login_tel">登录</a-button>
          </a-form-item>
        </a-form>
      </a-tab-pane>
      <a-tab-pane key="3" tab="邮箱登录">
        <a-form :model="mail" :wrapper-col="wrapperCol">
          <h1
            style="
              text-align: center;
              font-family: 楷体;
              font-size: 45px;
              font-weight: bold;
              color: #ffffff;
              margin: 30px auto;
            "
          >
            Login
          </h1>
          <a-form-item>
            <a-input v-model:value="mail.u_mail" placeholder="input your mail">
              <template #prefix><mail-outlined /></template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-search
              v-model:value="mail.code"
              placeholder="input the code"
              @search="getMailVerificationCode()"
            >
              <template #prefix><lock-outlined /></template>
              <template #enterButton>
                <a-button type="primary" v-if="mail_code" disabled
                  >已获得验证码</a-button
                >
                <a-button type="primary" v-else>获得验证码</a-button>
              </template>
            </a-input-search>
          </a-form-item>

          <a-form-item :wrapper-col="wrapperCol" style="padding-bottom: 50px">
            <a-button type="primary" block @click="login_mail">登录</a-button>
          </a-form-item>
        </a-form>
      </a-tab-pane>
    </a-tabs>
  </div>
  <div class="adminfooter">Community ©2021 Designed by ZHAO YU</div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { post, setLocalAToken, tip } from "@/common";
import { useRouter } from "vue-router";
import {
  UserOutlined,
  LockOutlined,
  MailOutlined,
  MobileOutlined,
} from "@ant-design/icons-vue";

const router = useRouter(); //获取路由器
const activeKey = ref("1");
const tel_code = ref(false);
const mail_code = ref(false);

const user = reactive({
  u_id: "",
  u_pwd: "",
});

const tel = reactive({
  u_tel: "",
  code: "",
});

const mail = reactive({
  u_mail: "",
  code: "",
});

const login = () => {
  post("/admin/login/byPwd", user).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalAToken(token);
    router.push({ path: "/admin/home" });
  });
};

const getTelVerificationCode = () => {
  console.log(tel.u_tel);
  post("/admin/login/getTelVerificationCode/" + tel.u_tel).then((res) => {
    tel_code.value = true;
  });
};

const getMailVerificationCode = () => {
  console.log(mail.u_mail);
  post("/admin/login/getMailVerificationCode/" + mail.u_mail).then((res) => {
    mail_code.value = true;
  });
};

const login_tel = () => {
  post("/admin/login/byTelVerification", tel).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalAToken(token);
    router.push({ path: "/admin/home" });
  });
};

const login_mail = () => {
  post("/admin/login/byMailVerification", mail).then((res) => {
    tip.success("登录成功！");
    const token = res.data;
    setLocalAToken(token);
    router.push({ path: "/admin/home" });
  });
};

//一行24列
const wrapperCol = {
  offset: 5,
  span: 14,
};
</script>

<style scoped>
.adminhead {
  width: 100%;
  height: 2cm;
}

.img-head {
  margin-left: 1.5cm;
  height: 100%;
}

.adminbody {
  position: flex;
  top: 2cm;
  bottom: 0px;
  left: 0px;
  right: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 16cm;
  background-image: url("/public/adminlogin.jpg");
  background-size: 1600px, 750px;
  background-repeat: no-repeat;
  background-position: center;
}

.adminfooter {
  text-align: center;
  margin-top: 3px;
}
</style>